<?php
/*
*
*	美财精品超市
*
*/

class official_shopControl extends MobileControl{
    private $mr_class_id = '288,274,285,287,281,282,293'; // 美容分类id
    private $mf_class_id = '253,257,258,263,292'; // 美容分类id
    private $mr_class_array = array ();
    private $mf_class_array = array ();
    private $class_array = array (); // 所有分类id 包括子分类id
    private $store_id = OFFICIAL_STORE_ID;
    private $store_class; // 商城分类数组
    public function __construct(){
        parent::__construct();
    }
    public function indexOp(){
        $this->classOp ();
        $lang = Language::getLangContent ();
        Language::read ( 'home_activity_index' );
        // 得到查询价格
        if (! empty ( $_GET ['price'] )) {
            $price = explode ( '-', $_GET ['price'] );
            if (intval ( $price [0] ) > 0 || intval ( $price [1] ) > 0) {
                $price_interval [0] = empty ( $price [0] ) ? 0 : $price [0];
                $price_interval [1] = empty ( $price [1] ) ? 0 : $price [1];
            }
            Tpl::output ( 'price_interval', $price_interval );
        }
        // 得到排序方式
        if (! empty ( $_GET ['key'] )) {
            $order_tmp = trim ( $_GET ['key'] );
            $order = '';
            switch ($order_tmp) {
                case 'sales' : // 销量
                    $order = 'salenum';
                    break;
                case 'credit' : // 信用
                    $order = 'store_credit';
                    break;
                case 'price' : // 价格升序
                    $order = 'goods_store_price';
                    break;
                case 'click' : // 浏览量
                    $order = 'goods_click';
                    break;
            }
            $order .= ' ' . trim ( $_GET ['order'] );
        }
        $ext_order = C ( 'promotion_allow' ) ? 'goods_sort desc,xianshi_flag desc,goods_starttime desc' : ' goods_sort desc';
        $order = trim($order) ? $order . ', goods_starttime desc' : $ext_order; // 设置排序

        if (! empty ( $_GET ['promotion'] )) {
            // 当前选项
            $promotion = '';
            switch ($_GET ['promotion']) {
                case 'xianshi' :
                    $promotion = $lang ['nc_xianshi'];
                    $xianshi_flag = 1;
                    break;
                case 'groupbuy' :
                    $promotion = $lang ['goods_class_index_groupbuy'];
                    $group_flag = 1;
                    break;
            }
            Tpl::output ( 'promotion', $promotion );
        }
        $param = array ();
        $param ['price'] = $price_interval;
        $param ['group_flag'] = $group_flag;
        $param ['xianshi_flag'] = $xianshi_flag;
        $param ['order'] = $order;
        $param ['store_id'] = $this->store_id;
        $param ['goods_show'] = 1;
        $param ['goods_state'] = 0;
        /* 全部按钮处理 */
        if (! empty ( $_GET ['class'] )) {
            if ($_GET ['class'] == 'mr') {
                $param ['stc_id_in'] = implode ( ',', $this->mr_class_array );
            } 
            else if($_GET ['class']=='supplier'){
                $supplier=true;//系列产品标记(1处修改)
            }else if($_GET['class']=='keycomment'){
                $keycomment=true;//系列产品标记(商品关键字推荐)(1处修改)
            }
            else {
                $param ['stc_id_in'] = implode ( ',', $this->mf_class_array );
            }
        } else {
            if ($_GET ['class_id']) {
                $param ['stc_id_in'] = $_GET ['class_id'];
                $model_store_class = new my_goods_classModel ();
                $stc_id_arr = $model_store_class->getChildAndSelfClass ( intval ( $_GET ['class_id'] ), '1' );
                if (is_array ( $stc_id_arr ) && count ( $stc_id_arr ) > 0) {
                    $param ['stc_id_in'] = implode ( ',', $stc_id_arr );
                } else {
                    $param ['stc_id'] = $stc_id_arr;
                }

                //新增属性筛选
                $class = Db::queryRow("SELECT stc_name,stc_attr_ids FROM #__store_goods_class WHERE stc_id = '{$_GET['class_id']}'");
                Tpl::output('class',$class);
                if(!empty($class['stc_attr_ids'])){
                    $new_attr = array();
                    $attr = Db::queryAll("SELECT * FROM #__store_os_attr WHERE attr_id IN({$class['stc_attr_ids']})");
                    if(!empty($attr) && is_array($attr)){
                        foreach($attr as $k=>$v){
                            $new_attr[$v['attr_id']]  = $v;
                            $new_attr[$v['attr_id']]['attr_values'] = Db::queryAll("SELECT * FROM #__store_os_attr_value WHERE attr_id = '{$v['attr_id']}'");
                        }
                        Tpl::output('attr_array',$new_attr);
                    }
                }
                $a_id = trim($_GET['a_id']);
                if(!empty($a_id)){
                    $checked_attr = array();
                    $res = Db::queryAll("SELECT s.*,a.* FROM #__store_os_attr_value AS s LEFT JOIN #__store_os_attr AS a ON s.attr_id = a.attr_id WHERE s.attr_value_id IN({$_GET['a_id']})");
                    if(!empty($res) && is_array($res)){
                        foreach($res as $k=>$v){
                            $checked_attr[$v['attr_id']] = $v;
                        }
                        Tpl::output('checked_attr',$checked_attr);
                    }
                    $param ['sca_attr_value_ids'] = str_replace(',', ' ', $a_id);
                }
            }else{
                $param ['stc_id_in'] =implode ( ',', $this->mr_class_array ).','. implode ( ',', $this->mf_class_array );
            }
        }
        $keyword = trim ( $_GET ['keyword'] );
        if ($keyword != '') {
            if (C ( 'fullindexer.open' )) {
                $data ['goods_id_str'] = searchModel::full_search( $keyword );
                $search_key = '';
            } else {
                $search_key = $keyword;
            }
        }
        $param ['keyword'] = $search_key;
        $param ['goods_id_in'] = $data ['goods_id_str'];
        $page = new Page ();
        $page->setEachNum ( 20 );
        $page->setStyle ( 'admin' );
        $goodsModel = new goodsModel ();
        if($supplier){
            //系列产品(2处修改)
            $param ['is_recommend_supplier'] = 1;
            $_GET['supplier_id'] && $param ['supplier_id'] = $_GET['supplier_id'];
            $goods_all = $goodsModel->getGoods ( $param, $page, 'goods.*,store_class_goods.stc_id', 'supplier' );//COUNT(DISTINCT goods.goods_id),
            $sql="SELECT * FROM #__ims_supplier WHERE is_recommend_supplier=1";
            $su_select=Db::queryAll($sql);
            Tpl::output ( 'su_select', $su_select);
        }else if($keycomment){
            //系列产品(商品关键字2处修改)
            $param ['is_keywords_commend'] = 1;
            $_GET['goods_keywords_commend'] && $param ['goods_keywords_commend'] = $_GET['goods_keywords_commend'];
            $goods_all = $goodsModel->getGoods ( $param, $page, 'goods.*,store_class_goods.stc_id', 'stc' );
            $sql = "SELECT goods_keywords_commend FROM #__goods WHERE is_keywords_commend = 1 GROUP BY goods_keywords_commend";
            $kg_select=Db::queryAll($sql);
            Tpl::output ( 'kg_select', $kg_select);
        }else{
            $goods_all = $goodsModel->getGoods ( $param, $page, 'goods.*,store_class_goods.stc_id', 'stc' );//COUNT(DISTINCT goods.goods_id),
        }
        if (! empty ( $goods_all ) && is_array ( $goods_all )) {
            if ($search_key) {
                // 得到商品数
                $goods_count = $page->getTotalNum ();
                $sql = "SELECT * FROM #__search_word WHERE keyword='$search_key'";
                $sw_info = db::queryRow ( $sql );
                if (! $sw_info) {
                    $sw_row = array ();
                    $sw_row ['keyword'] = $search_key;
                    $keyStr = SplitWord ( stripslashes ( str_replace ( array (
                        '"',
                        "'",
                        '+',
                        '-'
                    ), array (
                        '',
                        '',
                        '',
                        ''
                    ), $search_key ) ) );
                    $sw_row ['keyword_fulltext'] = $keyStr;
                    $sw_row ['search_times'] = 1;
                    $sw_row ['search_count'] = $goods_count;
                    $sw_row ['search_time'] = time ();
                    db::insert ( 'search_word', $sw_row );
                } else {
                    $sw_row = array ();
                    $keyStr = SplitWord ( stripslashes ( str_replace ( array (
                        '"',
                        "'",
                        '+',
                        '-'
                    ), array (
                        '',
                        '',
                        '',
                        ''
                    ), $search_key ) ) );
                    $sw_row ['keyword_fulltext'] = $keyStr;
                    $sw_row ['search_times'] = $sw_info ['search_times'] + 1;
                    $sw_row ['search_count'] = $goods_count;
                    $sw_row ['search_time'] = time ();
                    db::update ( 'search_word', $sw_row, "keyword='$search_key'" );
                }
            }
        }else{
            // 没有搜索到商品
            unset ($param ['keyword']);
            //系列产品(3处修改，如果系列产品为空，则不再默认查询) 
            if(!$supplier){
                $param ['keyword_not_match'] = $search_key;
                $goods_all = $goodsModel->getGoods($param, $page, 'goods.*,store_class_goods.stc_id', 'stc');
            }
        }
        $this->_assignMobileData($page);
        /*广告*/
        $now_time = time();//ap_id:415
        $sql = "SELECT * FROM #__adv WHERE ap_id = '415' AND is_allow = '1' AND adv_start_date < $now_time AND adv_end_date > $now_time ORDER BY slide_sort ASC";
        $adv = Db::queryAll($sql);
        Tpl::output('adv',$adv);
        Tpl::output ( 'goods_all', $goods_all );
        /* 商城内分类 */
        Tpl::output ( 'store_class', $this->store_class );
        Tpl::showpage ( 'official_shop');
    }

    /* 进行商城内分类处理 */
    public function classOp() {
        $this->mr_class_array = explode ( ',', $this->mr_class_id );
        $this->mf_class_array = explode ( ',', $this->mf_class_id );
        Tpl::output ( 'store_id', $this->store_id );
        $model_store_class = new my_goods_classModel ();
        $this->store_class = $model_store_class->getShowTreeList ( $this->store_id );
        Tpl::output ( 'store_class', $this->store_class );
        foreach ( $this->store_class as $k => $v ) {
            $this->class_array [] = $k;
            if (is_array ( $v ['children'] ) && count ( $v ['children'] ) > 0) {
                foreach ( $v ['children'] as $v1 ) {
                    if (in_array ( $k, $this->mr_class_array )) {
                        $this->mr_class_array [] = $v1 ['stc_id'];
                    }
                    if (in_array ( $k, $this->mf_class_array )) {
                        $this->mf_class_array [] = $v1 ['stc_id'];
                    }
                    $this->class_array [] = $v1 ['stc_id'];
                }
            }
        }
    }
}

?>